package com.sheng.util;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.sheng.whyclass.person.event.AddScoreEvent;
import com.sheng.whyclass.person.event.FindAllFinishedStudentInfoEvent;
import com.sheng.whyclass.person.event.FindAllStudentInfoEvent;
import com.sheng.whyclass.person.event.FindStudentInfoEvent;
import com.sheng.whyclass.person.event.FindTeacherInfoEvent;
import com.sheng.whyclass.person.event.StudentLoginEvent;
import com.sheng.whyclass.person.event.StudentRegisterEvent;
import com.sheng.whyclass.person.event.TeacherLoginEvent;
import com.sheng.whyclass.person.event.TeacherRegisterEvent;
import com.sheng.whyclass.person.model.BaseResult;
import com.sheng.whyclass.person.model.StudentIdInfoResp;
import com.sheng.whyclass.person.model.StudentInfoData;
import com.sheng.whyclass.person.model.StudentInfoResp;
import com.sheng.whyclass.person.model.TeacherInfoData;
import com.sheng.whyclass.work.event.AddAnswerEvent;
import com.sheng.whyclass.work.event.AddWorkEvent;
import com.sheng.whyclass.work.event.FindAllWorkEvent;
import com.sheng.whyclass.work.event.FindAnswerEvent;
import com.sheng.whyclass.work.event.FindFinishedEvent;
import com.sheng.whyclass.work.event.FindFinishedStudentEvent;
import com.sheng.whyclass.work.event.FindOneFinishedStudentEvent;
import com.sheng.whyclass.work.event.FindScoreEvent;
import com.sheng.whyclass.work.event.FindUnFinishedEvent;
import com.sheng.whyclass.work.event.SubmitAnswerEvent;
import com.sheng.whyclass.work.model.AnswerResp;
import com.sheng.whyclass.work.model.ContentData;
import com.sheng.whyclass.work.model.ContentStudentData;
import com.sheng.whyclass.work.model.OneStudentData;
import com.sheng.whyclass.work.model.ScoreResp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

public class HttpProcessManager {

    public static HttpProcessManager instance = null;

    public static HttpProcessManager getInstance() {
        if (null == instance) {
            synchronized (HttpProcessManager.class) {
                if (null == instance) {
                    instance = new HttpProcessManager();
                }
            }
        }
        return instance;
    }


    public StringRequest loginStudent(String url, final String student_name, final String student_pass) {
        StringRequest request = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        BaseResult result = BaseResult.getFromJson(res);
                        StudentLoginEvent baseEvent = new StudentLoginEvent(result.getCode(), result.getInfo());
                        CustomToast.showToast(result.toString());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new StudentLoginEvent(Constant.CODE_FAIL, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.ID, student_name);
                map.put(Constant.PASS, student_pass);
                return map;
            }

        };
        return request;
    }

    public StringRequest loginTeacher(String url, final String teacher_name, final String teacher_pass) {

        StringRequest request = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        BaseResult result = BaseResult.getFromJson(res);
                        TeacherLoginEvent baseEvent = new TeacherLoginEvent(result.getCode(), result.getInfo());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new TeacherLoginEvent(Constant.CODE_FAIL, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.ID, teacher_name);
                map.put(Constant.PASS, teacher_pass);
                return map;
            }

        };
        //EventBus.getDefault().post(new BaseEvent(Constant.CODE_FAIL, null));
        return request;
    }

    public StringRequest registerStudent(String url, final String student_name, final String student_pass, final String student_class, final String student_department) {

        StringRequest request = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        BaseResult result = BaseResult.getFromJson(res);
                        StudentRegisterEvent baseEvent = new StudentRegisterEvent(result.getCode(), result.getInfo());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new StudentRegisterEvent(Constant.CODE_FAIL, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.USER, student_name);
                map.put(Constant.PASS, student_pass);
                map.put(Constant.CLASS, student_class);
                map.put(Constant.DEPARTMENT, student_department);
                return map;
            }

        };
        return request;
    }

    public StringRequest registerTeacher(String url, final String teacher_name, final String teacher_pass, final String teacher_department) {

        StringRequest request = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        BaseResult result = BaseResult.getFromJson(res);
                        TeacherRegisterEvent baseEvent = new TeacherRegisterEvent(result.getCode(), result.getInfo());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new TeacherRegisterEvent(Constant.CODE_FAIL, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.USER, teacher_name);
                map.put(Constant.PASS, teacher_pass);
                map.put(Constant.DEPARTMENT, teacher_department);
                return map;
            }

        };
        return request;
    }


    public StringRequest findStudentInfo(String hostStudentFind, final String id) {
        StringRequest request = new StringRequest(Request.Method.POST, hostStudentFind,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        StudentInfoData result = StudentInfoData.getFromJson(res);
                        FindStudentInfoEvent baseEvent = new FindStudentInfoEvent(result.getCode(), result.getInfo(), result.getData());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new FindStudentInfoEvent(Constant.CODE_FAIL, null, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.ID, id);
                return map;
            }

        };
        return request;


    }

    public StringRequest findOneStudentInfo(String hostStudentFind, final String id) {
        StringRequest request = new StringRequest(Request.Method.POST, hostStudentFind,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        OneStudentData result = OneStudentData.getFromJson(res);
                        FindOneFinishedStudentEvent baseEvent = new FindOneFinishedStudentEvent(result.getCode(), null, (ArrayList<OneStudentData.DataBean>) result.getData());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new FindOneFinishedStudentEvent(Constant.CODE_FAIL, null, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.S_ID, id);
                return map;
            }

        };
        return request;


    }

    public StringRequest findAllStudentInfo(String hostStudentFind) {
        StringRequest request = new StringRequest(Request.Method.POST, hostStudentFind,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        StudentInfoResp result = StudentInfoResp.getFromJson(res);
                        FindAllStudentInfoEvent baseEvent = new FindAllStudentInfoEvent(result.getCode(), result.getInfo(), (ArrayList<StudentInfoResp.DataBean>) result.getData());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new FindAllStudentInfoEvent(Constant.CODE_FAIL, null, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                return map;
            }

        };
        return request;


    }


    public StringRequest findAllFinishedStudentInfo(String hostStudentFind) {
        StringRequest request = new StringRequest(Request.Method.POST, hostStudentFind,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        StudentIdInfoResp result = StudentIdInfoResp.getFromJson(res);
                        FindAllFinishedStudentInfoEvent baseEvent = new FindAllFinishedStudentInfoEvent(result.getCode(), result.getInfo(), (ArrayList<StudentIdInfoResp.DataBean>) result.getData());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new FindAllFinishedStudentInfoEvent(Constant.CODE_FAIL, null, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                return map;
            }

        };
        return request;


    }




    public StringRequest findTeacherInfo(String hostStudentFind, final String id) {
        StringRequest request = new StringRequest(Request.Method.POST, hostStudentFind,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        TeacherInfoData result = TeacherInfoData.getFromJson(res);
                        FindTeacherInfoEvent baseEvent = new FindTeacherInfoEvent(result.getCode(), result.getInfo(), result.getData());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new FindTeacherInfoEvent(Constant.CODE_FAIL, null, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.ID, id);
                return map;
            }

        };
        return request;
    }



    public StringRequest findStudentWork(String hostStudentFind, final String id) {
        StringRequest request = new StringRequest(Request.Method.POST, hostStudentFind,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        ContentData result = ContentData.getFromJson(res);
                        FindUnFinishedEvent baseEvent = new FindUnFinishedEvent(result.getCode(), result.getInfo(), result.getData());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new FindUnFinishedEvent(Constant.CODE_FAIL, null, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.ID, id);
                return map;
            }

        };
        return request;
    }

    public StringRequest findStudentWorkEd(String hostStudentFind, final String id) {
        StringRequest request = new StringRequest(Request.Method.POST, hostStudentFind,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        ContentData result = ContentData.getFromJson(res);
                        FindFinishedEvent baseEvent = new FindFinishedEvent(result.getCode(), result.getInfo(), result.getData());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new FindFinishedEvent(Constant.CODE_FAIL, null, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.ID, id);
                return map;
            }

        };
        return request;
    }

    public StringRequest AddWork(String hostStudentFind, final String title,final String content) {
        StringRequest request = new StringRequest(Request.Method.POST, hostStudentFind,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        BaseResult result = BaseResult.getFromJson(res);
                        AddWorkEvent baseEvent = new AddWorkEvent(result.getCode(), result.getInfo());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new AddWorkEvent(Constant.CODE_FAIL, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.TOPIC, title);
                map.put(Constant.CONTENT, content);
                return map;
            }

        };
        return request;
    }

    public StringRequest findWorkAnswer(String hostAnswerFind, final String id) {
        StringRequest request = new StringRequest(Request.Method.POST, hostAnswerFind,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        AnswerResp result = AnswerResp.getFromJson(res);
                        FindAnswerEvent baseEvent = new FindAnswerEvent(result.getCode(), null, result);
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new FindAnswerEvent(Constant.CODE_FAIL, null, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.CONTENT_ID, id);
                return map;
            }

        };
        return request;
    }

    public StringRequest findWorkScore(String hostAnswerFind, final String id, final String w_c_id) {
        StringRequest request = new StringRequest(Request.Method.POST, hostAnswerFind,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        ScoreResp result = ScoreResp.getFromJson(res);
                        FindScoreEvent baseEvent = new FindScoreEvent(result.getStaticX(),null,result);
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new FindScoreEvent(Constant.CODE_FAIL, null, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.S_ID, id);
                map.put(Constant.CONTENT_ID, w_c_id);
                return map;
            }

        };
        return request;
    }

    public StringRequest submitAnswer(String hostSubAnswer, final String id, final String s_id, final String answer) {
        StringRequest request = new StringRequest(Request.Method.POST, hostSubAnswer,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        ContentData result = ContentData.getFromJson(res);
                        SubmitAnswerEvent baseEvent = new SubmitAnswerEvent(result.getCode(), result.getInfo());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new SubmitAnswerEvent(Constant.CODE_FAIL, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.CONTENT_ID, id);
                map.put(Constant.S_ID, s_id);
                map.put(Constant.CONTENT_ANSWER, answer);
                return map;
            }

        };
        return request;


    }

    public StringRequest addAnswer(String hostSubAnswer, final String topic, final String answer) {
        StringRequest request = new StringRequest(Request.Method.POST, hostSubAnswer,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        ContentData result = ContentData.getFromJson(res);
                        AddAnswerEvent baseEvent = new AddAnswerEvent(result.getCode(), result.getInfo());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new AddAnswerEvent(Constant.CODE_FAIL, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.TOPIC, topic);
                map.put(Constant.CONTENT, answer);
                return map;
            }

        };
        return request;


    }


    public StringRequest findAllWork(String hostStudentWorkAll) {
        StringRequest request = new StringRequest(Request.Method.POST, hostStudentWorkAll,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        ContentData result = ContentData.getFromJson(res);
                        FindAllWorkEvent baseEvent = new FindAllWorkEvent(result.getCode(),result.getInfo(),result.getData());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new FindAllWorkEvent(Constant.CODE_FAIL, null,null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数
                Map<String, String> map = new HashMap<String, String>();
                return map;
            }

        };
        return request;
    }

    public StringRequest findFinishedStudent(String hostStudentWorkAll, final String w_c_id) {
        StringRequest request = new StringRequest(Request.Method.POST, hostStudentWorkAll,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        ContentStudentData result = ContentStudentData.getFromJson(res);
                        FindFinishedStudentEvent baseEvent = new FindFinishedStudentEvent(result.getCode(),result.getInfo(), (ArrayList<ContentStudentData.DataBean>) result.getData());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new FindFinishedStudentEvent(Constant.CODE_FAIL, null,null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数contentId
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.CONTENTS_ID, w_c_id);
                return map;
            }

        };
        return request;
    }

    public StringRequest addStudentScore(String hostStudentWorkAll, final String s_id,final String topic,final String score) {
        StringRequest request = new StringRequest(Request.Method.POST, hostStudentWorkAll,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String arg0) {
                        String res = com.sheng.util.TextUtils.stripSAE(arg0);
                        BaseResult result = BaseResult.getFromJson(res);
                        AddScoreEvent baseEvent = new AddScoreEvent(result.getCode(),result.getInfo());
                        EventBus.getDefault().post(baseEvent);
                        return;
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {


                EventBus.getDefault().post(new AddScoreEvent(Constant.CODE_FAIL, null));
                return;
            }


        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //传递参数contentId
                Map<String, String> map = new HashMap<String, String>();
                map.put(Constant.S_ID, s_id);
                map.put(Constant.TOPIC, topic);
                map.put(Constant.SCORE, score);
                return map;
            }

        };
        return request;
    }
}


